package com.hopes.bizz.risk.control.evaluate;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.risk.control.bo.NegatedItem;
import com.hopes.bizz.risk.control.bo.RiskEffectEvaluationScore;
import com.hopes.bizz.risk.control.bo.RiskEvaluateBo;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskEntity;
import com.hopes.bizz.risk.control.entity.RiskObjectEntity;
import com.hopes.bizz.risk.control.entity.RiskUnitEntity;
import com.hopes.bizz.risk.control.service.RiskMeasureTaskService;
import com.hopes.bizz.risk.control.service.RiskObjectService;
import com.hopes.bizz.risk.control.service.RiskUnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.hopes.bizz.risk.common.constant.RiskDicConstant.RISK_CHECK_CYCLE_TYPE_HOUR;

/**
 * @author lyb
 * @version 1.0
 * @description: 重大危险源排查任务清单中小时级任务均大于2小时。
 * @date 2023/7/12 13:26
 */

@Component
@Slf4j
@Order(1003)
public class RiskHazTimelinessRejectFactorHandler extends RiskRejectFactorHandler {

    @Resource
    private RiskObjectService riskObjectService;

    @Resource
    private RiskUnitService riskUnitService;

    @Resource
    private RiskMeasureTaskService riskMeasureTaskService;

    @Override
    protected NegatedItem doCalculate(RiskEvaluateBo riskEvaluateBo, NegatedItem negatedItem) {
        StringBuilder sb = new StringBuilder();
        boolean score = Boolean.TRUE;
        List<RiskObjectEntity> hazRiskObjects = getHazardRiskObject();

        if (CollectionUtil.isEmpty(hazRiskObjects)) {
            sb.append("不存在重大危险源!");
        }
        for (RiskObjectEntity hazRiskObject : hazRiskObjects) {
            List<RiskUnitEntity> riskUnitList = getRiskUnitByRiskObjectId(hazRiskObject.getId());
            List<Long> riskUnitIdList = riskUnitList.stream().map(RiskUnitEntity::getId).collect(Collectors.toList());
            List<RiskMeasureTaskEntity> riskMeasureTaskList = riskMeasureTaskService.getByRiskUnitIdList(riskUnitIdList);
            long count = riskMeasureTaskList.stream().filter(task -> {
                Integer checkCycle = task.getCheckCycle();
                String checkCycleType = task.getCheckCycleType();
                return checkCycle < 3 && RISK_CHECK_CYCLE_TYPE_HOUR.equals(checkCycleType);
            }).count();

            if (count == 0) {
                String hazardName = hazRiskObject.getHazardName();
                score = false;
                sb.append(hazardName).append(" 风险分析对象重大危险源不存在小时级任务或者小时任务均大于2小时!").append(LINE_SEPARATOR);
            }
        }
        negatedItem.setNegatedItemThree(score);
        if (sb.length() > 0) {
            negatedItem.setNegatedItemThreeReason(sb.toString());
        }

        return doNextCalculate(riskEvaluateBo, negatedItem);
    }

    private List<RiskObjectEntity> getHazardRiskObject() {
        LambdaQueryWrapper<RiskObjectEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RiskObjectEntity::getHazardSourceFlag, true);
        return riskObjectService.list(wrapper);
    }

    private List<RiskUnitEntity> getRiskUnitByRiskObjectId(Long riskObjectId) {
        LambdaQueryWrapper<RiskUnitEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RiskUnitEntity::getRiskObjectId, riskObjectId);
        return riskUnitService.list(wrapper);
    }

    @Override
    protected Boolean isUse(RiskEffectEvaluationScore riskEffectEvaluationScore) {
        return riskEffectEvaluationScore.getTaskTimelinessFlag();
    }
}
